package rental;

import users.*;
import lockers.*;
import java.util.Vector;
import OOJ.Console;

import security.Checkpoint.*;
import security.Session.*;

/**
 * 
 * @author erwin
 * 
 * Dit werkt waarschijnlijk beter als het een JUnit gebeuren gaat worden, test een stuk beter;
 *
 */
public class Program {
	
	private Vector<User> users;
	private Vector<Group> groups;
	private Vector<Locker> lockers;
	
	//private User currentUser;
	
	private Program() {
		this.users = new Vector<User>();
		this.groups = new Vector<Group>();
		this.lockers = new Vector<Locker>();
	}
	/**
	 * @param args - empty
	 * 
	 */
	public static void main(String[] args) {
		System.out.println("Lockers rental program test");
		System.out.println("---------------------------");
		
		//TODO:in aspect implementeren als het nodig is;
		//Checkpoint singletonCheckPoint = security.Checkpoint.Checkpoint.getCheckpoint();
		
		Program program = new Program();
		program.setup();
		
		
		
	}
	
	/**
	 * Setup waar bij 2 users elkaars kluisje proberen te openen. Geen authenticatietest alleen 
	 * authorisatie.
	 *
	 */
	private void setup() {
		this.run();
	}
	
	public void run() {
		String command;
		while(true) {
			
				command = Console.readString("LockerSys :> ");
				
				String[] commandTokens = command.split(" ");
				
			try {	
				// antwoorden
				//System.out.print("\n");
				
				if (command.equals("help")) {
					execHelp();
				} else if (commandTokens[0].equals("addUser")) {
					execAddUser(commandTokens[1]);
				} else if (commandTokens[0].equals("addPublicLocker")) {
					execAddPublicLocker(commandTokens[1]);
				} else if (commandTokens[0].equals("addOwnedLocker")) {
					execAddOwnedLocker(commandTokens[1], commandTokens[2]);
				} else if (commandTokens[0].equals("addGroup")) {
					execAddGroup(commandTokens[1]);
				} else if (commandTokens[0].equals("addToGroup")) {
					execAddToGroup(commandTokens[1], commandTokens[2]);
				} else if (commandTokens[0].equals("setLockerOwner")) {
					execSetLockerOwner(commandTokens[2], commandTokens[1]);
					
				} else if (commandTokens[0].equals("setItem")) {
					execSetItem(commandTokens[1], commandTokens[2]);					
				} else if (commandTokens[0].equals("getGroups")) {
					execGetGroups();
				} else if (commandTokens[0].equals("getUsers")) {
					execGetUsers();				
				} else if (commandTokens[0].equals("getLockers")) {
					execGetLockers();			
				} else if (commandTokens[0].equals("getGroupInfo")) {
					execGetGroupInfo(commandTokens[1]);				
				} else if (commandTokens[0].equals("getUserInfo")) {
					execGetUserInfo(commandTokens[1]);
				} else if (commandTokens[0].equals("getLockerInfo")) {
					execGetLockerInfo(commandTokens[1]);
				} else {
					System.out.println("Command not found");
				}
			} catch (Exception e) {
				System.out.println("Incorrect arguments - " + e.toString());
				System.out.println("--------------------------------");
				e.printStackTrace();
				System.out.println("--------------------------------");
			}
		}
	}
	
//	public User setCurrentUser(User user) {
//		this.currentUser = user;
//		return this.currentUser;
//	}
	
	private void execHelp() {
		System.out.println("Help");
		System.out.println("---------------------------");
		System.out.println("addUser <name>");
		System.out.println("addPublicLocker <identifier>");
		System.out.println("addOwnedLocker <identifier> <username>");
		System.out.println("addGroup <name>");
		System.out.println("---------------------------");
		System.out.println("addToGroup <user> <group>");
		System.out.println("setLockerOwner <user> <lockeridentifier>");		
		System.out.println("setItem <lockeridentifier> <contents>");
		System.out.println("---------------------------");
		System.out.println("getGroups");
		System.out.println("getUsers");
		System.out.println("getLockers");
		System.out.println("getGroupInfo <group>");
		System.out.println("getUserInfo <user>");
		System.out.println("getLockerInfo <identifier>");
	}
	private void execAddUser(String username) {
		User newUser = new User(username);
		this.users.add(newUser);
		System.out.println("User Added: " + newUser.toString());
	}

	private void execAddPublicLocker(String locker) {
		PublicLocker tmpLocker = new PublicLocker(locker);
		this.lockers.add(tmpLocker);	
		
	}

	private void execAddOwnedLocker(String locker, String username) {
		for (int i=0;i<users.size();i++){
			if (users.get(i).toString().equals(username)) {
				OwnedLocker tmpLocker = new OwnedLocker(locker, users.get(i));
				this.lockers.add(tmpLocker);
				
				System.out.println("OwnedLocker Added: " + users.get(i).toString() + " - " + locker);
				break;
			}
		}
	}

	private void execAddGroup(String groupname) {
		Group group = new Group(groupname);
		this.groups.add(group);	
		System.out.println("Group Added: " + group.getName());
	}

	private void execAddToGroup(String username, String group) {
		for (int i=0;i<users.size();i++){
			if (users.get(i).toString().equals(username)) {
				for (int k=0;k<groups.size();k++){
					if (groups.get(k).getName().equals(group)) {	
						
						groups.get(k).addUser(users.get(i));
						System.out.println("User: " + users.get(i).toString() + " added to group " + groups.get(k).getName());
						break;
					}
				}
				
			}
		}
	}

	private void execGetLockerInfo(String locker) {
		for (int j=0;j<lockers.size();j++){
			if (lockers.get(j).toString().equals(locker)) {
				if (lockers.get(j) instanceof OwnedLocker) {
					System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + ((OwnedLocker)lockers.get(j)).getOwner() + " - " + lockers.get(j).getItem());
				} else {
					System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + lockers.get(j).getItem());
				}
			}
		}
	}
	private void execGetUserInfo(String user) {
		for (int i=0;i<users.size();i++){
			if (users.get(i).toString().equals(user)) {
				System.out.println("User:" + users.get(i).toString());
				for (int j=0;j<lockers.size();j++){
					if (lockers.get(j) instanceof OwnedLocker) {
						if (((OwnedLocker)lockers.get(j)).getOwner().toString().equals(users.get(i).toString())) {
							System.out.println("OwnedLocker:" + lockers.get(j).toString() + " - " + lockers.get(j).getItem());
						}
					}
				}
			}
		}
	}
	private void execGetGroupInfo(String group) {
		for (int i=0;i<groups.size();i++){
			if (groups.get(i).toString().equals(group)) {
				System.out.println("Group: " + groups.get(i).getName());
				
				Vector users = groups.get(i).getUsers();
				for (int j=0;j<users.size();j++){
					System.out.println("User " + j + ": " + users.get(j).toString());
				}
				break;
			}
		}
	}
	private void execGetLockers() {
		for (int i=0;i<lockers.size();i++){
			System.out.println("Locker " + i + ": " + lockers.get(i).toString());
		}
	}
	private void execGetUsers() {
		for (int i=0;i<users.size();i++){
			System.out.println("User " + i + ": " + users.get(i).toString());
		}
	}
	private void execGetGroups() {
		for (int i=0;i<groups.size();i++){
			System.out.println("Group " + i + ": " + groups.get(i).getName());
		}
	}
	private void execSetItem(String locker, String item) {
		for (int i=0;i<lockers.size();i++){
			if (lockers.get(i).toString().equals(locker)) {
				lockers.get(i).setItem(item);
				System.out.println("setItem:" + item + " in locker " + lockers.get(i).toString());
				break;
			}	
		}
	}
	private void execSetLockerOwner(String locker, String user) {
		for (int i=0;i<users.size();i++){
			if (users.get(i).toString().equals(user)) {
				for (int j=0;j<lockers.size();j++){
					if (lockers.get(j).toString().equals(locker)) {
						if (lockers.get(j) instanceof OwnedLocker) {
							((OwnedLocker)lockers.get(j)).setOwner(users.get(i));
							System.out.println("User: " + users.get(i).toString() + " added to locker " + lockers.get(j).toString());
							break;
						}
					}
				}
				
			}
		}			
	}
}